import 'package:flutter/material.dart';

/// 富文本显示组件
/// 用于在详情页面正确显示富文本内容，包括图片
class RichTextDisplay extends StatelessWidget {
  final String content;
  final TextStyle? textStyle;
  final EdgeInsets? padding;

  const RichTextDisplay({
    super.key,
    required this.content,
    this.textStyle,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    if (content.isEmpty) {
      return Container(
        padding: padding,
        child: Text(
          '暂无内容',
          style: textStyle?.copyWith(color: Colors.grey[400]) ?? TextStyle(color: Colors.grey[400]),
        ),
      );
    }

    // 使用简化的富文本显示
    return Container(
      padding: padding,
      child: SimpleRichTextDisplay(
        content: content,
        textStyle: textStyle ?? const TextStyle(fontSize: 16, height: 1.5),
      ),
    );
  }
}

/// 简化的富文本显示组件（不依赖 flutter_quill）
class SimpleRichTextDisplay extends StatelessWidget {
  final String content;
  final TextStyle? textStyle;
  final EdgeInsets? padding;

  const SimpleRichTextDisplay({
    super.key,
    required this.content,
    this.textStyle,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    if (content.isEmpty) {
      return Container(
        padding: padding,
        child: Text(
          '暂无内容',
          style: textStyle?.copyWith(color: Colors.grey[400]) ?? TextStyle(color: Colors.grey[400]),
        ),
      );
    }

    return Container(
      padding: padding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _parseContent(content),
      ),
    );
  }

  /// 解析内容，将图片和文本分离
  List<Widget> _parseContent(String content) {
    final List<Widget> widgets = [];
    
    // 支持新的图片格式 [IMAGE:URL] 和旧的格式 [图片:URL]
    final imageRegex = RegExp(r'\[(?:IMAGE|图片):([^\]]+)\]');
    int lastIndex = 0;

    for (final match in imageRegex.allMatches(content)) {
      // 添加图片前的文本
      if (match.start > lastIndex) {
        final text = content.substring(lastIndex, match.start);
        if (text.isNotEmpty) {
          widgets.add(
            Text(
              text,
              style: textStyle ?? const TextStyle(fontSize: 16, height: 1.5),
            ),
          );
        }
      }

      // 添加图片
      final imageUrl = match.group(1)!;
      widgets.add(
        Container(
          margin: const EdgeInsets.symmetric(vertical: 8),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Image.network(
              imageUrl,
              fit: BoxFit.cover,
              width: double.infinity,
              errorBuilder: (context, error, stackTrace) {
                return Container(
                  height: 200,
                  color: Colors.grey[200],
                  child: const Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.image_not_supported, size: 50, color: Colors.grey),
                      SizedBox(height: 8),
                      Text('图片加载失败', style: TextStyle(color: Colors.grey)),
                    ],
                  ),
                );
              },
            ),
          ),
        ),
      );

      lastIndex = match.end;
    }

    // 添加剩余的文本
    if (lastIndex < content.length) {
      final text = content.substring(lastIndex);
      if (text.isNotEmpty) {
        widgets.add(
          Text(
            text,
            style: textStyle ?? const TextStyle(fontSize: 16, height: 1.5),
          ),
        );
      }
    }

    return widgets.isEmpty 
        ? [Text(content, style: textStyle ?? const TextStyle(fontSize: 16, height: 1.5))]
        : widgets;
  }
}